import { defineStore } from "pinia"
import {
  reqGetCategory1List,
  reqGetCategory2List,
  reqGetCategory3List,
} from "@/api/product/category"
import type {
  Numberable,
  CategoryStore,
} from "@/api/product/model/categoryModel"

export const useCategoryListStore = defineStore("categoryList", {
  state: (): CategoryStore => {
    return {
      category1Id: undefined,
      category2Id: undefined,
      category3Id: undefined,

      category1List: [],
      category2List: [],
      category3List: [],
    }
  },

  actions: {
    async getCategory1List() {
      const result = await reqGetCategory1List()
      this.category1List = result
    },
    async getCategory2List(category1Id: number) {
      this.category1Id = category1Id

      this.category2Id = undefined
      this.category3Id = undefined
      this.category3List = []

      const result = await reqGetCategory2List(category1Id)
      this.category2List = result
    },
    async getCategory3List(category2Id: number) {
      this.category2Id = category2Id

      this.category3Id = undefined

      const result = await reqGetCategory3List(category2Id)
      this.category3List = result
    },
    async setCategory3Id(category3Id: number) {
      this.category3Id = category3Id
    },
  },
})
